package com.bupt.kgplatform.Util;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;

import java.io.Serializable;

@Data
public class R implements Serializable {
    private static final long serialVersionUID = 7988823970938566753L;

    private Integer code;

    private String msg;

    private Object data;

    private long pages;

    private long total;

    protected long size;
    protected long current;

    private PageInfo pageInfo;

    private void setResultCode(ResultCode code) {
        this.code = code.code();
        this.msg = code.message();
    }

    //成功 不返回数据直接返回成功信息
    public static R ok() {
        R r = new R();
        r.setResultCode(ResultCode.SUCCESS);
        return r;
    }

    //成功 并且加上返回数据
    public static R ok(Object data) {
        R r = new R();
        r.setResultCode(ResultCode.SUCCESS);
        r.setData(data);
        return r;
    }

    public static R ok(Object data, PageInfo pageInfo) {
        R r = new R();
        r.setResultCode(ResultCode.SUCCESS);
        r.setData(data);
        r.setPageInfo(pageInfo);
        return r;
    }

    public static R ok(Object data, Object msg) {
        R r = new R();
        r.setResultCode(ResultCode.SUCCESS);
        r.setData(data);
        r.setMsg(msg.toString());
        return r;
    }

    //成功 自定义成功返回状态 加上数据
    public static R ok(ResultCode ResultCode, Object data) {
        R r = new R();
        r.setResultCode(ResultCode);
        r.setData(data);
        return r;
    }

    public static <T> R ok(Page<T> page) {
        R r = new R();
        r.setResultCode(ResultCode.SUCCESS);
        r.setData(page.getRecords());
        r.setTotal(page.getTotal());
        r.setPages(page.getPages());
        r.setSize(page.getSize());
        r.setCurrent(page.getCurrent());
        return r;
    }

    // 单返回失败的状态码
    public static R failure(ResultCode ResultCode, String msg) {
        R r = new R();
        r.setResultCode(ResultCode);
        r.setMsg(msg);
        return r;
    }

    // 返回失败的状态码 及 数据
    public static R failure(Object data) {
        R R = new R();
        R.setResultCode(ResultCode.FAILURE);
        R.setData(data);
        return R;
    }
}
